package com.clei.Y2023.M10.D13.packet;

import com.clei.utils.ByteUtil;

import java.util.concurrent.BlockingQueue;

/**
 * 消防串口包处理器
 *
 * @author Y
 * @date 2023-10-25
 */
public class ComPacketHandler implements PacketHandler {

    /**
     * buffer
     */
    byte[] buffer = new byte[128];

    /**
     * bufferIndex
     */
    int bufferIndex = 0;

    /**
     * 包处理
     *
     * @param msgQueue 阻塞队列
     * @param data     data
     */
    @Override
    public void packetHandle(BlockingQueue<String> msgQueue, byte[] data) {
        boolean hasEnd = false;

        byte[] res = null;
        for (int i = 0; i < data.length; i++) {
            this.buffer[this.bufferIndex++] = data[i];
            // 0x83
            if (-125 == data[i]) {
                hasEnd = true;
                res = new byte[this.bufferIndex];
                System.arraycopy(this.buffer, 0, res, 0, bufferIndex);
                bufferIndex = 0;
            }
        }
        if (hasEnd) {
            msgQueue.add(ByteUtil.toHex(res));
        }
    }
}
